package org.example.course.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.commonapi.domain.R;
import org.example.commonapi.domain.TaskDiscuss;
import org.example.commonapi.domain.TaskDiscussReview;
import org.example.commonapi.dto.LoginInfo;
import org.example.commonapi.dto.TaskDiscussReviewDto;
import org.example.commonapi.util.JwtUtil;
import org.example.commonapi.util.StringUtil;
import org.example.course.service.TaskDiscussReviewService;
import org.example.course.service.TaskDiscussService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;



@RestController
@RequestMapping("/course")
@Slf4j
public class TaskDiscussController {

    @Resource
    private TaskDiscussService taskDiscussService;

    @Resource
    private TaskDiscussReviewService taskDiscussReviewService;

    @Resource
    private JwtUtil jwtUtil;

    /**
     * 根据任务id获取讨论主题
     * @param taskId
     * @return
     */
    @GetMapping("/get/task/discuss/{taskId}")
    public R getTaskDiscuss(@PathVariable Long taskId){
        LambdaQueryWrapper<TaskDiscuss> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(TaskDiscuss::getTaskId,taskId);
        TaskDiscuss taskDiscuss=taskDiscussService.getOne(queryWrapper);
        return R.success(taskDiscuss);
    }

    /**
     * 插入讨论主题
     * @param taskDiscuss
     * @return
     */
    @PostMapping("/insert/task/discuss")
    public R insertTaskDiscuss(@RequestBody TaskDiscuss taskDiscuss){
        if(StringUtil.isAnyEmpty(taskDiscuss.getContent()))return R.error("主题讨论内容不能为空");
        taskDiscussService.save(taskDiscuss);
        return R.success(taskDiscuss);
    }


    /**
     * 获取评论区列表
     * @param discussId
     * @return
     */
    @GetMapping("/list/task/discuss/review/{discussId}")
    public R listTaskDiscussReview(@PathVariable Long discussId){
        LambdaQueryWrapper<TaskDiscussReview> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(TaskDiscussReview::getDiscussId,discussId);
        if(!taskDiscussReviewService.exists(queryWrapper))return R.success(null);
        List<TaskDiscussReviewDto> list=taskDiscussReviewService.listTaskDiscussReview(discussId);
        return R.success(list);
    }


    /**
     * 插入评论
     * @param taskDiscussReview
     * @return
     */
    @PostMapping("/insert/task/discuss/review")
    public R insertTaskDiscussReview(@RequestBody TaskDiscussReview taskDiscussReview, HttpServletRequest request){
        LoginInfo loginInfo=jwtUtil.parseJwt(request.getHeader("token"));
        taskDiscussReview.setUserId(loginInfo.getId());
        if(StringUtil.isAnyEmpty(taskDiscussReview.getContent()))return R.error("评论内容不能为空");
        taskDiscussReview.setCreateTime(LocalDateTime.now());
        taskDiscussReview.setUpdateTime(LocalDateTime.now());
        taskDiscussReviewService.save(taskDiscussReview);
        return R.success(taskDiscussReview);
    }

    /**
     * 删除评论
     * @param id
     * @return
     */
    @DeleteMapping("/delete/task/discuss/review/{id}")
    public R deleteTaskDiscussReview(@PathVariable Long id){
        taskDiscussReviewService.removeById(id);
        return R.success();
    }

}
